<?php

class Controller{ 
	function invoke_index(){

		$this->page["title"] = "individual project";
		$this->page["js"] = array("view/js/index.js");
		$this->page["page"] = "view/index.php";
		include "view/template.php";
	
	}
	
	function invoke_upload_files(){
		include "view/upload_files.php";
	}	
	
	function invoke_upload_textareas(){
		include "view/upload_textareas.php";
	}
	
	function invoke_upload_urls(){
		include "view/upload_urls.php";
	}
	
	function invoke_submit(){
		
		$type = $_POST["type"];
		$k = 0;
		$k = $_POST["k"];
		if ($type == 1){
			if ($_FILES["file1"]["error"] > 0 || $_FILES["file2"]["error"]>0){
				echo "Error: " . $_FILES["file1"]["error"] . "<br />";
			}
			else{
				$text1 = file_get_contents($_FILES["file1"]["tmp_name"]); 
				$text2 = file_get_contents($_FILES["file2"]["tmp_name"]);
			}	
		}
		else if ($type == 2){
			$text1 = $_POST["text1"];
			$text2 = $_POST["text2"];
			
		}
		else if ($type == 3){
			$url1 = $_POST["url1"];
			$url2 = $_POST["url2"];
			$page1 = "";
			$page2 = "";
			if ($url1 != ""){
				$page1 = file_get_contents($url1);
			}
			if ($url2 != ""){
				$page2 = file_get_contents($url2);
			}
			
			$text1 = strip_tags($page1);
			$text2 = strip_tags($page2);			
		}

		$this->page["text1"] = array();
		$this->page["text1"]["sentences"] = $this->count_sentence($text1);
		$this->page["text1"]["words"] = $this->count_words($text1);
		$this->page["text1"]["unique_words"] = $this->count_unique_word($text1);
		$this->page["text1"]["chars"] = $this->count_char_frequency($text1);
		$this->page["text1"]["topk"] = $this->count_topk_frequency($text1, $k);
		$this->page["text2"] = array();
		
		$this->page["text2"]["sentences"] = $this->count_sentence($text2);
		$this->page["text2"]["words"] = $this->count_words($text2);
		$this->page["text2"]["unique_words"] = $this->count_unique_word($text2);
		$this->page["text2"]["chars"] = $this->count_char_frequency($text2);
		$this->page["text2"]["topk"] = $this->count_topk_frequency($text2, $k);
		
		$this->page["similarity"] = $this->similarity($text1, $text2);
		
		$this->page["page"] = "view/result.php";
		$this->page["title"]="result";
		
		include "view/template.php";
	
	}
	

	function count_sentence($string){
	
		return substr_count($string, ".") + substr_count($string, "?") + substr_count($string, "!");
	
	}
	
	function count_words($string){
		return str_word_count($string, 0);
	}
	
	function count_unique_word($string){
		$frequency = $this->count_word_frequency($string);
		$num = 0;
		foreach ($frequency as $word=>$time){
			if ($time == 1){
				$num += 1;
			}
		}
		
		return $num;
	}
	
	function count_char_frequency($string){
		$frequency = array();
		$eng_char = array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z");
		foreach (count_chars(strtolower($string), 1) as $i=>$val){
			if (in_array(chr($i), $eng_char)){
				$frequency[chr($i)] = $val;
			}
			
		}
		return $frequency;
	}
	
	function count_topk_frequency($string, $k){
		$frequency = $this->count_word_frequency($string);
		arsort($frequency);

		$topk = array_slice($frequency, 0, $k, TRUE);
		return $topk;		
	}
	
	function similarity($string1, $string2){
		$union = str_word_count($string1, 0) + str_word_count($string2, 0);
		$frequency1 = $this->count_word_frequency($string1);
		$frequency2 = $this->count_word_frequency($string2);
		$intersection = $this->count_word_same($frequency1, $frequency2);
		return array($union, $intersection,($intersection/$union));
	}

	
	function count_word_frequency($string){
		
		$frequency = array_count_values(str_word_count($string, 1));
		return $frequency;
	}
	
	function count_word_same($freq1, $freq2){
		$same = 0;
		foreach ($freq1 as $word=>$time){
			if (array_key_exists($word, $freq2)){
				$same += min($time, $freq2[$word]) * 2;
			}
		}
		return $same;
	}
	
	function count_word_diff($freq1, $freq2){
		
		$diff = 0;
		foreach ($freq1 as $word=>$time){
			if (array_key_exists($word,$freq2)){
				$diff += abs($time - $freq2[$word]); 
			}
			else{
				$diff += $time;
			}
		}
		foreach ($freq2 as $word=>$time){
			if (! array_key_exists($word, $freq1)){
				$diff += $time;
			}
		}
		return $diff;
	}

}

?>